package com.ruoyi.H5Send.demo;

import com.alibaba.fastjson.JSON;
import com.ruoyi.H5Send.model.request.SmsSendRequest;
import com.ruoyi.H5Send.model.request.SmsVariableRequest;
import com.ruoyi.H5Send.model.response.SmsSendResponse;
import com.ruoyi.H5Send.model.response.SmsVariableResponse;
import com.ruoyi.H5Send.util.ChuangLanSmsUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;


import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 *
 * @author tianyh
 * @Description:普通短信发送
 */
@Component
public class SmsSendDemo {

    public static final String charset = "utf-8";
    // 用户平台API账号(非登录账号,示例:N1234567)
    public static String account = "N4865036";
    // 用户平台API密码(非登录密码)
    public static String pswd = "Tut1Bsv25m4aef";
    //请求地址请登录253云通讯自助通平台查看或者询问您的商务负责人获取
    public static String smsVariableRequestUrl = "https://smssh1.253.com/msg/variable/json";
    //短信内容
    public static  String msg = "【璞源探索】您的验证码为: {$var}";


    @Async("AsyncPool")
    public  String sendYZM(Long orderId,String phone) {


        //        String msg = "【璞源探索】{$var}  正在对您发起背调，  {$var}  进行授权中";
        //参数组
        String randomYZM = ChuangLanSmsUtil.getRandomYZM();

//        String params = "13643669790,测试店铺,ISA754DASXCA";
        String params = phone+","+randomYZM;
        //状态报告
        String report= "false";

        SmsVariableRequest smsVariableRequest=new SmsVariableRequest(account, pswd, msg, params, report);

        String requestJson = JSON.toJSONString(smsVariableRequest);

//        System.out.println("before request string is: " + requestJson);

        String response = ChuangLanSmsUtil.sendSmsByPost(smsVariableRequestUrl, requestJson);

//        System.out.println("response after request result is : " + response);

        SmsVariableResponse smsVariableResponse = JSON.parseObject(response, SmsVariableResponse.class);

//        System.out.println("response  toString is : " + smsVariableResponse);
//        String code = smsVariableResponse.getCode();
        return randomYZM;

    }

//    public static final String charset = "utf-8";
//    // 用户平台API账号(非登录账号,示例:N1234567)
//    public static String account = "N4865036";
//    // 用户平台API密码(非登录密码)
//    public static String password = "Tut1Bsv25m4aef";
//
//
//    public static String sendYZM(Long orderId,String phones) {
//        //请求地址请登录253云通讯自助通平台查看或者询问您的商务负责人获取
//        String smsSingleRequestServerUrl = "https://smssh1.253.com/msg/v1/send/json";
//        //生成四位验证码:
//        String randomYZM = ChuangLanSmsUtil.getRandomYZM();
//
//
//        // 短信内容
//        String msg = "【璞源探索】你好,你的验证码是："+randomYZM;
//        //手机号码
//        String phone = phones;
//        //状态报告
//        String report= "false";
//
//        SmsSendRequest smsSingleRequest = new SmsSendRequest(account, password, msg, phone,report);
//
//        String requestJson = JSON.toJSONString(smsSingleRequest);
//
//
//
//        String response = ChuangLanSmsUtil.sendSmsByPost(smsSingleRequestServerUrl, requestJson);
//
//
//        SmsSendResponse smsSingleResponse = JSON.parseObject(response, SmsSendResponse.class);
//        String code = smsSingleResponse.getCode();
//
//        if (!code.equals("0")) {
//            throw new SecurityException("短信发送失败:"+smsSingleResponse.getErrorMsg());
//        }
//
//        return randomYZM;
//    }
}
